﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NL.ADA.Common;
using NL.ADA.Logging;
using NL.ADA.UI;

namespace NL.ADA.ConfigService.UI
{
    public partial class _Default : System.Web.UI.Page
    {
        private static readonly AdaLogger _logger = new AdaLogger();
        private bool _insertMode;
        private bool _editMode;
        public _Default()
        {
            this.Page.Load += new EventHandler(Page_Load);
            this.Page.PreRender += new EventHandler(Page_PreRender);

        }
        void Page_PreRender(object sender, EventArgs e)
        {
            _insertMode = Convert.ToBoolean(InsertModeHiddenField.Value);
            _editMode = TheGridView.EditIndex > -1;
            AddNewRowButton.Enabled = !_insertMode && !_editMode;

        }
        protected void UpdatePanel_Load(object sender,EventArgs e)
        {
            //ApplicationDropDownList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                _logger.Log(LogEvent.EnterEvent);
                TheFormPanel.Visible = false;
                if(FormView1.CurrentMode == FormViewMode.Insert )
                {
                    ((TextBox) FormView1.FindControl("AppCodeTextBox")).Text = String.Empty;
                    ((TextBox)FormView1.FindControl("DescriptionTextBox")).Text = String.Empty;
                    
                }
                _logger.Log(LogEvent.LeaveEvent);
            }
            
        }


        protected void RowCommand(object sender, GridViewCommandEventArgs e)
        {
            _logger.Log(LogEvent.EnterEvent);

            switch (e.CommandName)
            {
                case "Cancel":
                    {
                        if (TheGridView.SelectedDataKey != null)
                            if (TheGridView.SelectedDataKey.Value.ToString() == InsertedRowDataKeyHiddenField.Value.ToString())
                            {
                                var gateway = new Gateway();
                                var result = gateway.Delete((int)TheGridView.SelectedDataKey.Value);
                                if (!result)
                                {
                                    throw new Exception(String.Format("new row could not be removed"));
                                }
                            }

                        if (InsertModeHiddenField != null) InsertModeHiddenField.Value = "false";
                        break;
                    }

                case "Update":
                    {
                        //TODO
                        InsertModeHiddenField.Value = "false";
                        break;
                    }
            }
            _logger.Log(LogEvent.LeaveEvent);
        }
        protected void FormViewItemCommand(object sender, FormViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Cancel":
                    {
                        TheFormPanel.Visible = false;
                        break;

                    }

            }
        }


        protected void AddNewRowButton_Click(object sender, EventArgs e)
        {
            _logger.Log(LogEvent.EnterEvent);
            _insertMode = true;
            var gateway = new Gateway();
            var tempAppId = 1;
            if(ApplicationDropDownList.SelectedValue != "0")
                tempAppId = Convert.ToInt32(ApplicationDropDownList.SelectedValue);
            
            var result = gateway.InsertConfig(0, 1, String.Empty, String.Empty,tempAppId,
                                              String.Empty, String.Empty, 1);

            InsertedRowDataKeyHiddenField.Value = result.ToString();
            InsertModeHiddenField.Value = "true";
            TheGridView.DataBind();
            TheGridView.SelectedIndex = TheGridView.Rows.Count - 1;
            TheGridView.SelectedRow.Focus();
            TheGridView.EditIndex = TheGridView.Rows.Count - 1;

            _logger.Log(LogEvent.LeaveEvent);
        }

        protected void AddNewApplicationButton_Click(object sender, EventArgs e)
        {
            TheFormPanel.Visible = true;
            FormView1.DefaultMode = FormViewMode.Insert;
        }

        protected void CancelLinkButtonClick(object sender, EventArgs e)
        {
            TheFormPanel.Visible = false;
        }

        protected void ApplicationDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            TheGridView.DataBind();
        }

        protected void ApplicationDropDownList_DataBound(object sender, EventArgs e)
        {
            ((DropDownList) sender).Items.Insert(0,new ListItem("All...","0"));
            
        }

        protected void ModeRadioButtonList_DataBound(object sender, EventArgs e)
        {
            ((RadioButtonList)sender).Items.Insert(0, new ListItem("All...", "0"));

        }
        protected void FormView_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            ApplicationDropDownList.DataBind();
        }
    }
}